package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private DishDao dishDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public void save(Category category) {
        categoryDao.save(category);
    }

    @Override
    public Page<Category> findByPage(Integer pageNum, Integer pageSize) {
        List<Category> categoryList = categoryDao.findAll(null);
        return new Page<>(categoryList);
    }

    @Override
    public void deleteById(Long id) {
        Long count = dishDao.findDishCountByCategoryId(id);
        if (count>0){
            throw new CustomException("删除失败，关联了菜品无法删除");
        }
        long count1 = setmealDao.findSetMealCountByCategoryId(id);
        if (count1>0){
            throw new CustomException("删除失败，关联了套餐无法删除");
        }
        categoryDao.deleteById(id);
    }

    @Override
    public void update(Category category) {
        categoryDao.update(category);
    }

    @Override
    public List<Category> findAll(Integer type) {
        return categoryDao.findAll(type);
    }
}
